<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class CSecurity{
	
	private static $diff = 86400000;	private static function _encryUrl($name,$time){
		return md5(Yii::app()->params["app"]['key'].$time.$name);
	}
	
	public static function getForgetpasswdUrl($userName){
		$time = time();
		$key = self::_encryUrl($userName,$time);
		$url = Yii::app()->params['app']['host']."?r=site/modifypassword&key=".$key."&time=".$time."&name=".$userName;
		return $url;
	}
	
	public static function validateForgetpasswdUrl($key,$userName,$time){
       $oriKey = self::_encryUrl($userName,$time);
       if($key!=$oriKey){
       	  return false;
       }
       if((time()-(int)$time)>self::$diff){
       	 return false;
       }
       return true;
	}
	
	public static function encrypt($text){
		$key = Yii::app()->params["app"]['key']; 
		return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $text, MCRYPT_MODE_CBC, md5(md5($key))));
	}
	
	public static function decrypt($ciphertext){
		$key = Yii::app()->params["app"]['key']; 
		return rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($ciphertext), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
		
	}
	
	
	public static function signPasswd($passwd, $salt){
		return CSecret::passSign($passwd, $salt);
	}
	
	
	public static function encryPasswd($passwd){
		return md5($passwd);
	}
}